//
//  Mine_TopicViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 2016/12/1.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Mine_TopicViewController.h"

#import "Mine_TopicMineTableViewCell.h"
#import "Topic_SearchViewController.h"

@interface Mine_TopicViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView * view_Head;
@property (nonatomic, strong) UILabel * lbl_mine;
@property (nonatomic, strong) UILabel * lbl_topic;
@property (nonatomic, strong) UIView * view_Line;

//
@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, assign) BOOL is_select;

@end

@implementation Mine_TopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_headView];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"我的话题";
    _topView.backgroundColor = navi_bar_bg_color;
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 100, 15)];
    lbl_return.text = @"我";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 100, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //...
    UIImageView * image_Setting = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 35, 32, 20, 20)];
    image_Setting.image = [UIImage imageNamed:@"Search_white.png"];
    [_topView addSubview:image_Setting];
    
    UIButton * btn_Setting = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_Setting.frame = CGRectMake(SCREEN_WIDTH - 10 - 35, 24.5, 35, 35);
    [_topView addSubview:btn_Setting];
    [btn_Setting addTarget:self action:@selector(SettingAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//
//右侧...
- (void)SettingAction:(UIButton *)sender
{
//    NSLog(@"点击进入搜索页面");
    
    Topic_SearchViewController * topic_SearchViewController = [[Topic_SearchViewController alloc] init];
    [self showViewController:topic_SearchViewController sender:nil];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 50, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[Mine_TopicMineTableViewCell class] forCellReuseIdentifier:@"cell_mine"];
    
    [self.tableView registerClass:[Mine_TopicMineTableViewCell class] forCellReuseIdentifier:@"cell_topic"];
}

#pragma mark - tableView代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.is_select == 0)
    {
        return 1;
    }
    else
    {
        return 5;
    }
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.is_select == 0)
    {
        Mine_TopicMineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_mine" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.name.text = @"#外勤审批#";
        cell.number.text = @"2345";
        
        cell.btn_select.layer.borderColor = [UIColor grayColor].CGColor;
        cell.btn_select.tag = indexPath.row + 100;
        [cell.btn_select setTitle:@"取消关注" forState:(UIControlStateNormal)];
        [cell.btn_select setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [cell.btn_select addTarget:self action:@selector(btn_selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        return cell;
    }
    else
    {
        Mine_TopicMineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_topic" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.name.text = @"#战报#";
        cell.number.text = [NSString stringWithFormat:@"%ld24",indexPath.row + 1];
        
        cell.btn_select.layer.borderColor = [UIColor orangeColor].CGColor;
        cell.btn_select.tag = indexPath.row + 1000;
        [cell.btn_select setTitle:@"关注" forState:(UIControlStateNormal)];
        [cell.btn_select setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
        [cell.btn_select addTarget:self action:@selector(btn_select1Action:) forControlEvents:(UIControlEventTouchUpInside)];
        
        return cell;
    }
}

#pragma mark - 点击关注
- (void )btn_selectAction:(UIButton *)sender
{
//    NSInteger count = sender.tag - 100;
//    NSLog(@"%ld",count);
    
    NSLog(@"取消关注");
}

- (void )btn_select1Action:(UIButton *)sender
{
//    NSInteger count = sender.tag - 1000;
//    NSLog(@"%ld",count);
    
    NSLog(@"关注");
}

























#pragma mark - 头视图
- (void )p_headView
{
    self.view_Head = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 50)];
    self.view_Head.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.view_Head];
    
    //
    self.lbl_mine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH / 2, 50)];
    self.lbl_mine.text = @"我关注的";
    self.lbl_mine.textAlignment = NSTextAlignmentCenter;
    self.lbl_mine.textColor = [UIColor orangeColor];
    [self.view_Head addSubview:self.lbl_mine];
    
    //
    self.lbl_topic = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 50)];
    self.lbl_topic.text = @"热门话题";
    self.lbl_topic.textAlignment = NSTextAlignmentCenter;
    self.lbl_topic.textColor = [UIColor grayColor];
    [self.view_Head addSubview:self.lbl_topic];

    //
    self.view_Line = [[UIView alloc] init];
    self.view_Line.frame = CGRectMake(0, 48, SCREEN_WIDTH / 2, 2);
    self.view_Line.backgroundColor = [UIColor orangeColor];
    [self.view_Head addSubview:self.view_Line];
    
    UIButton * btn_mine = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn_mine.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 50);
    [self.view_Head addSubview:btn_mine];
    [btn_mine addTarget:self action:@selector(btn_mineAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton * btn_topic = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn_topic.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 50);
    [self.view_Head addSubview:btn_topic];
    [btn_topic addTarget:self action:@selector(btn_topicAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 点击头视图
- (void )btn_mineAction:(UIButton *)sender
{
    self.is_select = 0;
    self.lbl_mine.textColor = [UIColor orangeColor];
    self.lbl_topic.textColor = [UIColor grayColor];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.view_Line.frame = CGRectMake(0, 48, SCREEN_WIDTH / 2, 2);
    } completion:^(BOOL finished) {
       
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            [self.tableView reloadData];
        });
    }];
}

- (void )btn_topicAction:(UIButton *)sender
{
    self.is_select = 1;
    self.lbl_mine.textColor = [UIColor grayColor];
    self.lbl_topic.textColor = [UIColor orangeColor];

    [UIView animateWithDuration:0.5 animations:^{
        self.view_Line.frame = CGRectMake(SCREEN_WIDTH / 2, 48, SCREEN_WIDTH / 2, 2);
    } completion:^(BOOL finished) {
       
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            [self.tableView reloadData];
        });
    }];
}


@end
